package njust.cms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import njust.tables.admin.TbAdmin;
import njust.tables.admin.TbAdminDAO;
import njust.tables.student.TbStudent;
import njust.tables.student.TbStudentDAO;
import njust.tables.teacher.TbTeacher;
import njust.tables.teacher.TbTeacherDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Main 
{
	@Resource(name="TbStudentDAO")
	private TbStudentDAO studentdao;
	
	@Resource(name="TbTeacherDAO")
	private TbTeacherDAO teacherdao;
	
	@Resource(name="TbAdminDAO")
    private TbAdminDAO admindao;
	
	@RequestMapping("/main")
	public String main(HttpServletRequest request)
	{
		return "main";
	}
	
	@RequestMapping("/main_student")
	public String main_student(HttpServletRequest request)
	{
		return "main_student";
	}
	
	@RequestMapping("/main_teacher")
	public String main_teacher(HttpServletRequest request)
	{
		return "main_teacher";
	}
	
	@RequestMapping("/main_admin")
	public String main_admin(HttpServletRequest request)
	{
		return "main_admin";
	}
	
	@RequestMapping("info")
	public String info(HttpServletRequest request)
	{
		return "info_home";
	}
		
	
	@RequestMapping("/example")//规范化样例,按照这个模板来写页面
	public String example(HttpServletRequest request)
	{
		return "example";
	}
	
	@RequestMapping("/test")
	public String test(HttpServletRequest request)
	{
		return "test";
	}
	
	@RequestMapping("/404")
	public String page404(HttpServletRequest request)
	{
		return "page-404";
	}
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request)
	{
		return "login";
	}
	
	@RequestMapping("/checkin")
	public String checkin(HttpServletRequest request,String user_id,String password)
	{
		HttpSession session = request.getSession();
		TbAdmin myadmin=null;
		TbStudent mystudent=null;
		TbTeacher myteacher=null;
		if(admindao.findByAdminId(user_id).size()==1)
			myadmin =  (TbAdmin)admindao.findByAdminId(user_id).get(0);
		else if(studentdao.findByUserId(user_id).size()==1)
			mystudent = (TbStudent)studentdao.findByUserId(user_id).get(0);
		else if(teacherdao.findByUserId(user_id).size()==1)
			myteacher = (TbTeacher)teacherdao.findByUserId(user_id).get(0);
		if(myadmin!=null&&myadmin.getPassword().equals(password))
		{
			session.setAttribute("Name", myadmin.getAdminName());
			session.setAttribute("Id", user_id);
			session.setAttribute("isLogined", "true");
			session.setAttribute("Role", "admin");
			return "redirect:admin/main";
		}
		else if(mystudent!=null && mystudent.getPassword().equals(password))
		{
			session.setAttribute("Name", mystudent.getUserName());
			session.setAttribute("Id", user_id);
			session.setAttribute("isLogined", "true");
			session.setAttribute("Role", "student");
			return "redirect:main_student";
		}
		else if(myteacher!=null && myteacher.getPassword().equals(password))
		{
			session.setAttribute("Name", myteacher.getUserName());
			session.setAttribute("Id", user_id);
			session.setAttribute("isLogined", "true");
			session.setAttribute("Role", "teacher");
			return "redirect:main_teacher";
		}
		else
		{
			session.setAttribute("Name", "未登录");
			session.setAttribute("Id", "NULL");
			session.setAttribute("isLogined", "error");
			session.setAttribute("Role", "NULL");
			return "redirect:login";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		session.removeAttribute("isLogined");
        session.removeAttribute("Name");
        session.removeAttribute("Id");
        session.removeAttribute("Role");
		return "redirect:login";
	}
	
	@RequestMapping("/modify")//个人信息修改
	public String modify(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		String user_id = (String)session.getAttribute("Id");
		String Role = (String)session.getAttribute("Role");
		if(Role.equals("admin"))
		{
			TbAdmin temp = (TbAdmin)admindao.findByAdminId(user_id).get(0);
			request.setAttribute("myadmin", temp);
			return "modify_admin";
		}
		else if(Role.equals("student"))
		{
			TbStudent temp =(TbStudent)studentdao.findByUserId(user_id).get(0);
			request.setAttribute("stu", temp);
			return "modify_student";
		}
		else
		{
			TbTeacher temp =(TbTeacher)teacherdao.findByUserId(user_id).get(0);
			request.setAttribute("t", temp);
			return "modify_teacher";
		}
	}
	
	@RequestMapping("/modify_submit_admin")//管理员信息修改
	public String modify_submit_admin(HttpServletRequest request,Integer id,String admin_name,String password)
	{
		TbAdmin temp = admindao.findById(id);
		temp.setAdminName(admin_name);
		temp.setPassword(password);
		admindao.merge(temp);
		HttpSession session = request.getSession();
		session.setAttribute("Name", admin_name);
		return "redirect:admin/main";
	}
	
	@RequestMapping("/modify_submit_student")//学生信息修改
	public String modify_submit_student(HttpServletRequest request,Integer id,String tel,String email,String password)
	{
		TbStudent temp = studentdao.findById(id);
		temp.setTel(tel);
		temp.setEmail(email);
		temp.setPassword(password);
		studentdao.merge(temp);
		return "redirect:main_student";
	}
	
	@RequestMapping("/modify_submit_teacher")//教师信息修改
	public String modify_submit_teacher(HttpServletRequest request,Integer id,String tel,String email,String password)
	{
		TbTeacher temp = teacherdao.findById(id);
		temp.setTel(tel);
		temp.setEmail(email);
		temp.setPassword(password);
		teacherdao.merge(temp);
		return "redirect:main_teacher";
	}
}
